//
//  MJMainTabBarController.swift
//  新浪微博
//
//  Created by MaJi on 16/4/6.
//  Copyright © 2016年 MaJi. All rights reserved.
//

import UIKit

class MJMainTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

//        1.自定义tabBar
//        注意是只读不能赋值
//        self.tabBar = MJTabBar()
        
        //使用KVC
//        tabBar.classForCoder是查询类名
//        print(tabBar.classForCoder)
        let mainTabBar = MJTabBar()
        setValue(mainTabBar, forKey: "tabBar")
//        1.1给加号按钮添加一个点击事件
        mainTabBar.plusBtn.addTarget(self,action:"plusBtnDidClick", forControlEvents:.TouchUpInside)
//        print(tabBar.classForCoder)
//        2.添加子控制器
       addChildViewControlers()
    }

//    实现加号按钮的点击  注意swift是静态语言识别不到运行时的方法
//    @objc将方法变为运行时方法
   @objc private func plusBtnDidClick(){
        print("加号按钮被点击啦")
    let compostView = MJComposeView()
    compostView.show(self)
    
//    let plusView = MJComposeView()
//    UIApplication.sharedApplication().keyWindow?.addSubview(plusView)
    }
    private func addChildViewControlers(){
        addViewController(MJHomeTableViewController(), title: "首页", imageName: "tabbar_home",index: 0)
        addViewController(MJMessageController(), title: "消息", imageName: "tabbar_message_center",index: 1)
        addViewController(MJDiscoverController(), title: "发现", imageName: "tabbar_discover",index: 2)
        addViewController(MJProfileController(), title: "我", imageName: "tabbar_profile",index: 3)
    }
    
    private func addViewController(vc:UIViewController , title:String , imageName:String , index:Int) {
//        1.设置字体
//        vc.tabBarItem.title = title
//        vc.navigationItem.title = title
//        如果二者title相同可以这样写
        //        替换自定义的TabBarItem
        vc.tabBarItem = MJTabBarItem()
        vc.title = title
        vc.tabBarItem.tag = index + baseIndex
        vc.tabBarItem.image = UIImage(named: imageName)
//        2.imageWithRenderingMode(.AlwaysOriginal)设置图片颜色不被系统的渲染
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(.AlwaysOriginal)
//        3.设置字体颜色
//        第一中方式(可以设置每个item的颜色)
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: .Selected)
//        第二种方式,(所有的字体都一样)
//        tabBar.tintColor = UIColor.orangeColor()
//        添加到UINavigationController
        let nav = MJBaseNavigationController(rootViewController: vc)
//        添加到tabBar控制器
        addChildViewController(nav)
    }
//    点击tabBarItem的图片的动画
    override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
        print(item.tag)
        var index:Int = 0
            for btn in tabBar.subviews {
                if btn.isKindOfClass(NSClassFromString("UITabBarButton")!){
                    if index == item.tag - baseIndex {
                        //可以定位到点击的 UITabBarButton
                        //遍历UITabBarButton子视图
                        for v in btn.subviews {
                            if v.isKindOfClass(NSClassFromString("UITabBarSwappableImageView")!){
                                print(v)
                                print("终于找到你")
//                                执行动画效果
                                v.transform = CGAffineTransformMakeScale(0.5, 0.5)
                                UIView.animateWithDuration(0.25, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 2, options: [], animations: { () -> Void in
                                    v.transform = CGAffineTransformMakeScale(1, 1)
                                    }, completion: nil)
                            }
                        }
                    }
                    index++
                }
            }
        
    }
    
    
}
